/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.account;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.account.SysPermitMenu;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-12-11
 */
@Service("syspermitmenuService")
public class SysPermitMenuServiceImpl implements SysPermitMenuService {
    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of SysPermitMenu
     ****************************************/
    public void insert(SysPermitMenu syspermitmenu) throws Exception {
        iDataSet4iBatis.insert(syspermitmenu);
    }

    /****************************************
     * Update a instace of SysPermitMenu
     ****************************************/
    public void update(SysPermitMenu syspermitmenu) throws Exception {
        iDataSet4iBatis.update(syspermitmenu);
    }

    /****************************************
     * Delete a instace of SysPermitMenu
     ****************************************/
    public void delete(SysPermitMenu syspermitmenu) throws Exception {
        iDataSet4iBatis.delete(syspermitmenu);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(List<?> syspermitmenus) throws Exception {
        List<BaseModel> syspermitmenuList = new ArrayList<BaseModel>();
        for (Object obj : syspermitmenus) {
            SysPermitMenu sysPermitMenu = new SysPermitMenu();
            BeanUtils.populate(sysPermitMenu, (Map<?, ?>) obj);
            sysPermitMenu.setStatmentId("SysPermitMenu.insert");
            syspermitmenuList.add(sysPermitMenu);
        }
        SysPermitMenu sysPermitMenu = new SysPermitMenu();
        sysPermitMenu.setStatmentId("SysPermitMenu.delete");
        BeanUtils.populate(sysPermitMenu, (Map<?, ?>) syspermitmenus.get(0));
        iDataSet4iBatis.delete(sysPermitMenu);
        iDataSet4iBatis.batchInsert(syspermitmenuList);
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(SysPermitMenu syspermitmenu) throws Exception {
        String statementId = syspermitmenu.getStatmentId();
        String mainId = syspermitmenu.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> syspermitmenuList = new ArrayList<BaseModel>();
        for (String id : ids) {
            SysPermitMenu model = new SysPermitMenu();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            syspermitmenuList.add(model);
        }
        iDataSet4iBatis.batchDelete(syspermitmenuList);
    }

    /****************************************
     * Query instaces of SysPermitMenu
     ****************************************/
    public List<BaseModel> queryAsList(SysPermitMenu syspermitmenu) throws Exception {
        String queryListStatement = "";
        if (syspermitmenu.isPaging()) {
            queryListStatement = syspermitmenu.getStatmentId();
            syspermitmenu.setStatmentId("SysPermitMenu.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(syspermitmenu);
            syspermitmenu.setTotalCount(baseModel.getTotalCount());
            syspermitmenu.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(syspermitmenu);
    }

    /****************************************
     * Query a instaces of SysPermitMenu
     ****************************************/
    public BaseModel queryAsObject(SysPermitMenu syspermitmenu) throws Exception {
        return iDataSet4iBatis.queryAsObject(syspermitmenu);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
